<template>
  <block>
  <view class="search-box">
  <uni-search-bar :radius="100" cancelButton="none" @input="input"></uni-search-bar>
  </view>
  
  <!-- 搜索建议列表 -->
  <view class="sugg-list"  v-if="searchResults.length !== 0">
      <navigator class="sugg-item" v-for="(item,i) in searchResults" :key="i" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
          <view class="goods-name">{{item.goods_name}}</view>
          <uni-icons type="forward" size="14"></uni-icons>       
      </navigator>
  </view>
  <!-- 搜索历史列表 -->
 <!-- 搜索历史 -->
 <view class="history-box" v-else>
   <!-- 标题区域 -->
   <view class="history-title">
     <text>最近搜索</text>
     <uni-icons type="trash" size="17" @click="clean"></uni-icons>
   </view>
   <!-- 列表区域 -->
   <view class="history-list">
     <uni-tag :text="item" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(kw)"></uni-tag>
   </view>
 </view>
  </block>
</template>

<script>
  export default {
    data() {
      return {
        //搜索关键词
        kw:'', 
        
        timer:null,
        searchResults:[],
        //搜索历史列表
        historyList:['铁板烧牛排堡','麦麦脆汁鸡']
      };
    },
    onLoad(){
       this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods:{
     input(e){
        clearTimeout(this.timer)
        this.timer=setTimeout(()=>{
      this.kw=e.valueOf()
      this.getSearchList()
        },1500) 
    },
    
      //根据关键词搜索商品建议列表
      async getSearchList(){
        if(this.kw===''){
          this.searchResults=[]
          return 
        }
        console.log(res)
        //发起请求，获取建议列表
      const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
          this.searchResults = res.message
           this.saveSearchHistory()
      },
      saveSearchHistory(){
        // this.historyList.push(this.kw)
         // 1. 将 Array 数组转化为 Set 对象
          const set = new Set(this.historyList)
          // 2. 调用 Set 对象的 delete 方法，移除对应的元素
          set.delete(this.kw)
          // 3. 调用 Set 对象的 add 方法，向 Set 中添加元素
          set.add(this.kw)
          // 4. 将 Set 对象转化为 Array 数组
          this.historyList = Array.from(set)
            uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      //清空历史记录
      clean(){
        this.historyList=[]
        uni.setStorageSync('kw','[]')
      },
      gotoGoodsList(kw){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    },
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    }
  }

</script>

<style lang="scss">
.search-box{
  background-color: #00aaff;
  position: sticky;
  top: 0;
  z-index: 999;
}
.sugg-list{
  padding: 5px 10px;
}
.sugg-item{
   font-size: 12px;
     padding: 13px 0;
     border-bottom: 1px solid #efefef;
     display: flex;
     align-items: center;
     justify-content: space-between;
       .goods-name {
            // 文字不允许换行（单行文本）
            white-space: nowrap;
            // 溢出部分隐藏
            overflow: hidden;
            // 文本溢出后，使用 ... 代替
            text-overflow: ellipsis;
            margin-right: 3px;
          }
}
.history-title{
  margin-top: 10px;
  padding: 0px 15px;
  font-size: 14px;
  display: flex;
  justify-content: space-between; 
}
.history-list{
  display: flex;
  flex-wrap: wrap;
  .uni-tag{
    color: #000000;
    background-color: #efefef;
    display: block;
    margin-top: 10px;
    margin-right: 10px;
    border-radius: 10px;
  }
}

</style>
